<?php
namespace Weibo\Controller;
use Common\Controller\AdminbaseController;

/**
 * 举报后台分类管理控制器
 * @author Hongwei 2016-12-26
 */
class AdminReportController extends AdminbaseController {

	public function _initialize() {

	}

	/**
	 * 举报首页
	 * @param string $status	要列出的状态 all全部举报| handled 已处理| handleing 未处理
	 */
	public function index($status = 'all'){

		$report_model = D('WeiboReport');
		$db_prefix=C('DB_PREFIX');

		$where = array();
		$status == 'handleing' && $where[$db_prefix.'weibo_report.status'] = 0;	//未处理
		$status == 'handled' && $where[$db_prefix.'weibo_report.status'] = 1;		//已处理

		//页码
		$count = $report_model -> where($where) -> count();
		$page = $this -> page($count, 10);

		//列表
		$list = $report_model
			-> join('__WEIBO_REPORTCATE__ on __WEIBO_REPORT__.cate_id=__WEIBO_REPORTCATE__.cate_id')
			-> join('__WEIBO_USER__ on __WEIBO_REPORT__.user_id=__WEIBO_USER__.user_id')
			-> field('*')
			-> where($where)
			-> limit($page->firstRow.','.$page->listRows)
			-> order(array('report_id'=>'desc'))
			-> select();

		$this -> assign('status', $status);
		$this -> assign('page', $page -> show('Admin'));
		$this -> assign('list', $list);
		$this -> display ();
	}


	/**
	 * 举报编辑
	 */
	public function edit(){
		$this -> _modify();
	}


	/**
	 * 举报删除
	 */
	public function delete(){
		$model = M('WeiboReport');
		$report_id = I('report_id');
		$result = $model -> delete($report_id);

		$data = array(
			'status' => $result ? true : false,
			'info' => $result ? '' : '删除失败，ID：'.$report_id,
			'url' => '',
		);
		$this -> ajaxReturn($data);
	}

	/**
	 * 修改公用方法
	 */
	private function _modify() {
		$report_model = M('WeiboReport');	//pay attention
		$report_id = I('report_id');

		if (IS_POST) {
			//根据需要增加减少
			$validate = array(
				//array('feedback', 'require', '站方反馈不能为空'),
			);
			$result = array('status' => TRUE, 'info' => '', 'url' => ''); //这个什么意思TODO

			$report_model -> field('feedback');	//pay attention

			if ($report_model -> validate($validate) -> create()) {
				if (!$report_id) {
					$insert_id = $result =$report_model -> add();
					if ($insert_id) {
						$this -> success('添加成功');
					} else {
						$this -> success('添加失败');
					}
				} else {
					if($report_model -> where(array('report_id' => $report_id)) -> save()) {
						$this -> success('修改成功');
					} else {
						$result['status'] = FALSE;
						$this -> success('修改失败');
					}
				}
			} else {
				//返回错误提示
				$result['status'] = FALSE;
				$result['info'] = $report_model -> getError();
			}

			if ($result['status']) {
				$this -> success($result['info']);
			} else {
				$this -> error($result['info']);
			}
		}

		$report = $report_id ? $report_model -> find($report_id) : array();

		$this -> assign('report', $report);
		$this -> display(':AdminReport/form');	//pay attention
	}

	
}